import { OrderCards } from './orderCard';
let _OrderCards = OrderCards;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import OrderCardMock from './mk/mng/computed/orderCardMock';
 * OrderCardMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/computed/orderCard/listKey').reply(config => {
      let params = config.params;
      let mockOrderCards = _OrderCards.filter(orderCard => {
      	if(params.userCardCode||params.userCardCode==''){ if( orderCard.userCardCode.indexOf(params.userCardCode)>=0) return true;}
      	if(params.orderId||params.orderId==''){ if( orderCard.orderId.indexOf(params.orderId)>=0) return true;}
      	if(params.reduceMoney||params.reduceMoney==''){ if( orderCard.reduceMoney.indexOf(params.reduceMoney)>=0) return true;}
      	if(params.increaseBonus||params.increaseBonus==''){ if( orderCard.increaseBonus.indexOf(params.increaseBonus)>=0) return true;}
      	if(params.reduceBonus||params.reduceBonus==''){ if( orderCard.reduceBonus.indexOf(params.reduceBonus)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( orderCard.deptid.indexOf(params.deptid)>=0) return true;}
      	if(params.id||params.id==''){ if( orderCard.id.indexOf(params.id)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( orderCard.cardPkId.indexOf(params.cardPkId)>=0) return true;}
      	if(params.cardName||params.cardName==''){ if( orderCard.cardName.indexOf(params.cardName)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockOrderCards
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/computed/orderCard/list').reply(config => {
      let params = config.params;
      let mockOrderCards = _OrderCards.filter(orderCard => {
      	var isTrue=false;
      	if(params.userCardCode||params.userCardCode==''){if(orderCard.userCardCode.indexOf(params.userCardCode)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.orderId||params.orderId==''){if(orderCard.orderId.indexOf(params.orderId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceMoney||params.reduceMoney==''){if(orderCard.reduceMoney.indexOf(params.reduceMoney)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.increaseBonus||params.increaseBonus==''){if(orderCard.increaseBonus.indexOf(params.increaseBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceBonus||params.reduceBonus==''){if(orderCard.reduceBonus.indexOf(params.reduceBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(orderCard.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(orderCard.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(orderCard.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardName||params.cardName==''){if(orderCard.cardName.indexOf(params.cardName)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockOrderCards
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockOrderCards.length;
      mockOrderCards = mockOrderCards.filter((orderCard, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockOrderCards,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/computed/orderCard/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _OrderCards = _OrderCards.filter(orderCard => orderCard.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/computed/orderCard/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _OrderCards = _OrderCards.filter(orderCard => !params.includes(orderCard.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/computed/orderCard/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { userCardCode,orderId,reduceMoney,increaseBonus,reduceBonus,deptid,id,cardPkId,cardName } = params;
      _OrderCards.some(orderCard => {
        if (orderCard.id === id) {
			orderCard.userCardCode = userCardCode;
			orderCard.orderId = orderId;
			orderCard.reduceMoney = reduceMoney;
			orderCard.increaseBonus = increaseBonus;
			orderCard.reduceBonus = reduceBonus;
			orderCard.deptid = deptid;
			orderCard.id = id;
			orderCard.cardPkId = cardPkId;
			orderCard.cardName = cardName
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/computed/orderCard/add').reply(config => {
      var params = JSON.parse( config.data );
      let { userCardCode,orderId,reduceMoney,increaseBonus,reduceBonus,deptid,id,cardPkId,cardName } = params;
      _OrderCards.push({
		userCardCode:userCardCode,
		orderId:orderId,
		reduceMoney:reduceMoney,
		increaseBonus:increaseBonus,
		reduceBonus:reduceBonus,
		deptid:deptid,
		id:id,
		cardPkId:cardPkId,
		cardName:cardName
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};